import java.util.Arrays;

public class Solution2191 {
    public int[] sortJumbled(int[] mapping, int[] nums) {
        int[][] f=new int[nums.length][2];
        for (int i=0;i<nums.length;i++){
            f[i][0]=i;
            StringBuilder stringBuilder=new StringBuilder();
            String s=String.valueOf(nums[i]);
            for (char c : s.toCharArray()) {
                stringBuilder.append(mapping[c-'0']);
            }
            f[i][1]=Integer.parseInt(stringBuilder.toString());
        }
        Arrays.sort(f,(a, b)->(a[1]==b[1]?a[0]-b[0]:a[1]-b[1]));
        int[] ans=new int[nums.length];
        for (int i=0;i<nums.length;i++){
            ans[i]=nums[f[i][0]];
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution2191().sortJumbled(new int[]{8, 9, 4, 0, 2, 1, 3, 5, 7, 6}, new int[]{991, 338, 38})));
    }
}
